package com.ykdl.member.kid.circle;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.GroupsBean;
import com.ykdl.member.kid.beans.Listinfobean;
import com.ykdl.member.kid.beans.Memberbean;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.circle.AllCircleActivity.ViewHolder;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.views.MyGridView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class CircleInfoActivity extends BaseScreen{
	private TextView group_name,group_creat_time,group_admin,group_notice,membernum;
	private String group_id ;
	private MyGridView member_gridview ;
	private final String TAG= "CircleInfoActivity";
	private MemberAdapter adapter ;
	private Memberbean adminbean ;
	private ArrayList<UserActorBean>  memberlist;
	private ImageView head ;
	private BitmapUtils bitmapUtil ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("圈子介绍", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		group_id =getIntent().getStringExtra("group_id");
		setContent(R.layout.circleinfoactivity_xml);
		bitmapUtil = new BitmapUtils(this);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_icon);
		inint();
		getGroupData(group_id);
	}
	public void inint() {
		adapter = new MemberAdapter();
		head = (AsyncImageView)findViewById(R.id.head);
		group_name = (TextView)findViewById(R.id.group_name);
		group_creat_time = (TextView)findViewById(R.id.group_creat_time);
		group_admin = (TextView)findViewById(R.id.group_admin);
		group_notice = (TextView)findViewById(R.id.group_notice);
		membernum = (TextView)findViewById(R.id.membernum);
		member_gridview = (MyGridView)findViewById(R.id.member_gridview);
		
		
		getGroupMemberData(group_id);
		member_gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CircleInfoActivity.this,MemberListActivity.class);
				intent.putExtra("groupid", group_id);
				CircleInfoActivity.this.startActivity(intent);
				
			}
		});
	}
	public void getGroupData(String groupid) {
		showProgress("正在加载请稍等...");
		String  pageurl = KidConfig.GET_TOPICS_ANDPOSTS+groupid;
		Map<String,String> map = new HashMap<String, String>();
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,null);
		 TaskManager.startHttpRequest(request, new getGroupDataTag(),  Listinfobean.class);
	}
	class getGroupDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "Ok", 1).show();
			if (t instanceof Listinfobean) {
				Listinfobean bean  =(Listinfobean)t; 
				setData(bean);
				setadmin(bean.getCreated_actor());
			}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void getGroupMemberData(String groupid) {
		showProgress("正在加载请稍等...");
		String  pageurl = KidConfig.GET_GROUP_MEMBER+groupid;
		Map<String,String> map = new HashMap<String, String>();
		map.put("count", "5");
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,null);
		 TaskManager.startHttpRequest(request, new getGroupMemberTag(),  Memberlistbean.class);
	}
	class getGroupMemberTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof Memberlistbean) {
				finishProgress();
				Memberlistbean bean = (Memberlistbean)t;
				memberlist = bean.getList();
				adapter.setList(memberlist);
				adapter.notifyDataSetChanged();
				member_gridview.setAdapter(adapter);
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void setData(Listinfobean bean) {
		group_name.setText(bean.getGroup_name());
//		group_creat_time.setText(DateUtil.getDateDataSting(bean.getCreated_time()));
		group_notice.setText(bean.getDescription());
		membernum.setText("("+bean.getMember_count()+")");
	}
	public void setadmin (final UserActorBean bean ) {
		if (bean==null) {
			group_admin.setVisibility(View.INVISIBLE);
			head.setVisibility(View.INVISIBLE);
		}else {
			if (bean.getAvatar_file_meta()!=null) {
				String url = bean.getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.display(head, url);
				head.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(mContext, PersonCenterActivity.class);
						intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, bean.getActor_id());
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						mContext.startActivity(intent);
					}
				});
			}
			group_admin.setText(bean.getDisplay_name());
		}
		
	}
	class MemberAdapter extends BaseAdapter {
		ArrayList<UserActorBean>   list;
		
		public void setList(ArrayList<UserActorBean> list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			MemberViewHolder holder;
			if (convertView==null) {
				convertView = RelativeLayout.inflate(mContext,
						R.layout.member_gridview_item_xml, null);
				holder = new MemberViewHolder();
				holder.member_head = (ImageView) convertView.findViewById(R.id.member_head);
				convertView.setTag(holder);
			}else {
				holder = (MemberViewHolder) convertView.getTag();
			}
			if (list.get(position).getAvatar_file_meta()!=null) {
					String url = list.get(position).getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
					bitmapUtil.display(holder.member_head, url);
			}
			
			
			return convertView;
		}
		class MemberViewHolder {
			ImageView member_head;
		}

		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
}
